package com.superzqb.asus.superzqbFragment.superzqbJobShop.Shop;


import android.graphics.Color;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbUtil.PagerSlidingTabStrip;

import java.util.List;


/**
 * Created by asus on 2017/1/21.
 */

public class CampusShop extends FragmentActivity {
    private ImageButton back;
    private EditText in;
    private ViewPager mViewPager;
    /**
     * PagerSlidingTabStrip的实例
     */
    private PagerSlidingTabStrip tabs;

    /**
     * 获取当前屏幕的密度
     */
    private DisplayMetrics dm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.job_campus_shop_search);
        initView();//初始化视图
        initEvents();//初始化事件

    }
    /**
     * 对PagerSlidingTabStrip的各项属性进行赋值。
     */
    private void setTabsValue() {
        // 设置Tab是自动填充满屏幕的
        tabs.setShouldExpand(true);
        // 设置Tab的分割线是透明的
        tabs.setDividerColor(Color.TRANSPARENT);
        // tabs.setDividerColor(Color.BLACK);
        // 设置Tab底部线的高度
        tabs.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        tabs.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));// 4
        // 设置Tab标题文字的大小
        tabs.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm)); // 16
        // 设置Tab Indicator的颜色
        tabs.setIndicatorColor(Color.parseColor("#1296db"));// #45c01a
        // 设置选中Tab文字的颜色 (这是我自定义的一个方法)
        tabs.setSelectedTextColor(Color.parseColor("#1296db"));// #45c01a
        // 取消点击Tab时的背景色
        tabs.setTabBackground(0);
    }

    private void initEvents() {
        in.setOnClickListener(listen_top);
        back.setOnClickListener(listen_top);
        in.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if( hasFocus){
                    //Toast.makeText(CampusShop.this,"捕获焦点",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
    private void initView() {
        dm = getResources().getDisplayMetrics();
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
        mViewPager.setAdapter(new CampusShop.MyPagerAdapter(getSupportFragmentManager()));
        tabs.setViewPager(mViewPager);
        setTabsValue();
        back=(ImageButton)findViewById(R.id.bk);
        in=(EditText)findViewById(R.id.search);
    }
    public class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        private final String[] titles = {"全部", "闲置课本", "文体数码", "服装", "日用百货", "我能", "代劳", "二手"};


        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }
        @Override
        public Fragment getItem(int position) {
           return select(position);
        }
    }
    private Fragment select(int position){
        Fragment defaultFragment;
        Bundle bundle = new Bundle();
        if(position==0)
            defaultFragment=new CampusShopAll();
        else
            defaultFragment=new CampusShopOther();
        if(position!=0) {
            bundle.putInt("type", position+1);
            defaultFragment.setArguments(bundle);
        }
        return defaultFragment;
    }

    View.OnClickListener listen_top=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.bk:
                    finish();
                    break;
                case R.id.search:/*这里做搜索功能*/
                   // Toast.makeText(CampusShop.this,"捕获焦点",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        in.clearFocus();
        in.setFocusable(false);
    }
}
